cd ~/project-lwherry/Replication
/************************************************************************************
PROGRAM: RWMKM_REStat_cps_eligibility.do
PURPOSE: Calculates eligibility discontinuity size by state and race for 
		 Wherry, Miller, Kaestner, and Meyer. 
DATE: 	 January 30, 2017
NOTES:	 To run, (1) change directory above to a local directory. 
		 (2) Create subfolders called src, data, and output. 
		 (3) Put cpsunicon_march8188, afdcrules7996, afdcrulesjuly1996, and
	 	 medschiprules data files in src directory. 
		 (4) Put rd_medicaid5.ado in the local directory.
CONTACT: Laura Wherry, LWherry@mednet.ucla.edu
*************************************************************************************/
set more off
/******************************************************************************
A) PREPARE MARCH CPS DATA FROM 1981-1988 SURVEY YEARS
Note: The code to prepare the CPS data and impute public eligibility
builds on code originially written by Ambarish Chandra for analysis in 
LoSasso and Meyer (2006). However, this code incorporates some changes in the 
definitions of family, income, and work that are different than those used by AC, 
and expands the eligibility calculator to cover years 1979-87 and 1997-2009.
******************************************************************************/

use src/cpsunicon_march8188, clear
	
*rename variables
rename state state2
rename wkslyr wkswrk
rename hrslyr hrswrk
rename incwag wages 
rename incfrm farm 
rename incse selfemp 
rename faminc finc
rename fmipa famafdc

*In 1976-1988, the primary family members are coded as '0' (not
*in a sub or secondary family) and only other relatives in
*secondary families (those families not related to the HH head)
*are coded as '4'.	
recode famrel (4=5) (0=4)

*create family id, FAMID2
gen svyyr=_year
egen hhid=concat(svyyr hhseq)
egen famid2=concat(hhid fampos)

keep state2 age marstat sex famrel wgt wkswrk ///
hrswrk farm wages selfemp famid2 finc famafdc lineno race _race svyyr
numlabel, a

*recoding March Supplemental Weight for implied decimal points
replace wgt=wgt/100

destring svyyr, replace
gen yeart=svyyr-1

*create demographic variables
gen female=1*(sex==2)
gen married=1*(inlist(marstat,1,2)) 

*create hours variable that indicate hours of work in the last year
gen hours=wkswrk*hrswrk
replace hours=0 if hours==.

*emp measures whether you were in the labor force last year 
*i.e., positive hours of work
gen emp=1*(hours>0)
replace emp=0 if hours==.

*recoding of individual-level income variables
foreach v of var wages farm selfemp{
	*setting income levels to zero for those not in universe
	replace `v'=0 if wkswrk==99 | wkswrk==0
	replace `v'=0 if `v'<0
	replace `v'=0 if `v'==.
} 	

*recoding family-level income variable
replace finc=finc-famafdc
replace finc=0 if finc<0 | finc==.

*earn is total earnings for the individual
gen earn=(wages+farm+selfemp)

*identify head and spouse of family
sort famid2 famrel lineno
gen head=1*(famid2!=famid2[_n-1])
gen spouse=1*(famrel==2)
by famid2 famrel: gen newline=_n
replace spouse=1 if newline==2 & married & inrange(svyyr,1981,1988)
gen femhead=1*(head & female & ~married)

*defining child for AFDC purposes, must be:
*	(1) 0-17 using age as of July in the given year, and
*	(2) either a primary family or subfamily (related or unrelated)
*		member, and not a primary family (subfamily) head or spouse
gen kidafdc=1*(inrange(age,0,17) & inlist(famrel,3,4) & ~head & ~spouse)

*defining child under a given age dummy variable, using age as of 
*July in the given year, must be:
*	(1) either a primary family or subfamily (related or unrelated)
*		member, and not a primary family (subfamily) head or spouse
gen kage0=1*(age>=-1 & age<0 & inlist(famrel,3,4) & ~head & ~spouse)
forval y=1/19{
	gen kage`y'=1*(age>=0 & age<`y' & inlist(famrel,3,4) & ~head & ~spouse)
}
gen kidage=age if inlist(famrel,3,4) & ~head & ~spouse

*defined as child under any condition
gen kid=1*(kidafdc==1 | kage0==1 | kage19==1)
gen knage0=1*(age>=0)

*calculating number of kids of given ages in family	
bysort famid2: egen dum=min(kidage)
drop kidage
rename dum kidage
foreach y of var kid kidafdc kage0-kage19 {
	bysort famid2: egen s`y'=total(`y')
}
bysort famid2: egen famsize=total(knage0)

*calculate total earned income for family
bysort famid2: egen fearn=total(earn)

*calculate number of working head/spouses in family, WORKERS
gen shemp=emp*(head | spouse)
bysort famid2: egen workers=total(shemp)

*assign marital status & max hours of work to family & # of afdc kids to family
foreach y of var married hours {
	bysort famid2: egen dum=max(`y')
	drop `y'
	rename dum `y'
}

*create sample for eligibility calculation, children under age 18
keep if age<18
*only keep kids who are reported to be the child or other relative in the family
keep if inlist(famrel,3,4) & ~head & ~spouse

keep famid2 age female famsize state2 skage0 skage18 ///
skidafdc married head spouse femhead wgt yeart hours earn ///
fearn finc workers famafdc race _race
su

*adjust all dollar amounts to 1980 dollars using CPI-U
*ftp://ftp.bls.gov/pub/special.requests/cpi/cpiai.txt
foreach x of var finc fearn {
	replace `x'=`x'*(82.4/90.9) if yeart==1981
	replace `x'=`x'*(82.4/96.5) if yeart==1982
	replace `x'=`x'*(82.4/99.6) if yeart==1983
    replace `x'=`x'*(82.4/103.9) if yeart==1984
    replace `x'=`x'*(82.4/107.6) if yeart==1985
	replace `x'=`x'*(82.4/109.6) if yeart==1986
	replace `x'=`x'*(82.4/113.6) if yeart==1987
}
*express family income as percent of pov
foreach x of var finc fearn {
	gen fpl`x'=.
	replace fpl`x'=`x'/(3790+(famsize-1)*1220)*100
	replace fpl`x'=`x'/(4760+(famsize-1)*1520)*100 if state2==94
	replace fpl`x'=`x'/(4370+(famsize-1)*1400)*100 if state2==95
	drop `x'
}
*save cleaned CPS dataset
save data/ccps8188, replace

*keep variables needed for simulation
keep state2 famsize skage0 skage18 workers ///
skidafdc married hours fplfinc fplfearn wgt _race race

*set up variables for cohort simulation
gen birthday=.
gen bmonth=.
gen byear=.

*implement assumptions for eligibility determination
gen ccd=0

*initialize variables used in eligibility determination
gen date=.
gen pregnant=0
gen minc=.
gen mearn=.
gen fpl=.
gen mfpl=.
gen year=.
gen iobs=_n
save data/rd_simstatepre, replace

/******************************************************************************
B) CREATE PUBLIC ELIGIBILITY IMPUTATION PROGRAM
See WMKM_ReStat_eligibility_info.doc for more detailed information 
******************************************************************************/

capture program drop rd_eligibility
program define rd_eligibility
		*assign FPL for family size by eligibility date
		*source: Table 3.E8, Poverty guidelines for families of specified size, 
		*1965–2009 (in dollars) USSA, Office of Retired and Disability Policy,
		*Annual Statistical Supplement 2009
		*http://www.ssa.gov/policy/docs/statcomps/supplement/2009/3e.html#table3.e8
		replace fpl=3400+(famsize-1)*1100 if inrange(date,7905,8003)
		replace fpl=3790+(famsize-1)*1220 if inrange(date,8004,8102)
		replace fpl=4310+(famsize-1)*1380 if inrange(date,8103,8203)
		replace fpl=4680+(famsize-1)*1540 if inrange(date,8204,8301)
		replace fpl=4860+(famsize-1)*1680 if inrange(date,8302,8401)
		replace fpl=4980+(famsize-1)*1740 if inrange(date,8402,8502)
		replace fpl=5250+(famsize-1)*1800 if inrange(date,8503,8601)
		replace fpl=5360+(famsize-1)*1880 if inrange(date,8602,8701)
		replace fpl=5500+(famsize-1)*1900 if inrange(date,8702,8801)
		replace fpl=5770+(famsize-1)*1960 if inrange(date,8802,8901)
		replace fpl=5980+(famsize-1)*2040 if inrange(date,8902,9001)
		replace fpl=6280+(famsize-1)*2140 if inrange(date,9002,9101)
		replace fpl=6620+(famsize-1)*2260 if inrange(date,9102,9201)
		replace fpl=6810+(famsize-1)*2380 if inrange(date,9202,9301)
		replace fpl=6970+(famsize-1)*2460 if inrange(date,9302,9401)
		replace fpl=7360+(famsize-1)*2480 if inrange(date,9402,9501)
		replace fpl=7470+(famsize-1)*2560 if inrange(date,9502,9601)
		replace fpl=7740+(famsize-1)*2620 if inrange(date,9602,9702)
		replace fpl=7890+(famsize-1)*2720 if inrange(date,9703,9802)
		replace fpl=8050+(famsize-1)*2800 if inrange(date,9803,9903)
		replace fpl=8240+(famsize-1)*2820 if inrange(date,9904,10002)
		replace fpl=8350+(famsize-1)*2900 if inrange(date,10003,10102)
		replace fpl=8590+(famsize-1)*3020 if inrange(date,10103,10201)
		replace fpl=8860+(famsize-1)*3080 if inrange(date,10202,10301)
		replace fpl=8980+(famsize-1)*3140 if inrange(date,10302,10401)
		replace fpl=9310+(famsize-1)*3180 if inrange(date,10402,10502)
		replace fpl=9570+(famsize-1)*3260 if inrange(date,10503,10601)
		replace fpl=9800+(famsize-1)*3400 if inrange(date,10602,10701)
		replace fpl=10210+(famsize-1)*3480 if inrange(date,10702,10801)

		*Alaska, 94
		replace fpl=4270+(famsize-1)*1370 if inrange(date,7905,8003) & state2==94
		replace fpl=4760+(famsize-1)*1520 if inrange(date,8004,8102) & state2==94
		replace fpl=5410+(famsize-1)*1720 if inrange(date,8103,8203) & state2==94
		replace fpl=5870+(famsize-1)*1920 if inrange(date,8204,8301) & state2==94
		replace fpl=6080+(famsize-1)*2100 if inrange(date,8302,8401) & state2==94
		replace fpl=6240+(famsize-1)*2170 if inrange(date,8402,8502) & state2==94
		replace fpl=6560+(famsize-1)*2250 if inrange(date,8503,8601) & state2==94
		replace fpl=6700+(famsize-1)*2350 if inrange(date,8602,8701) & state2==94
		replace fpl=6860+(famsize-1)*2380 if inrange(date,8702,8801) & state2==94
		replace fpl=7210+(famsize-1)*2450 if inrange(date,8802,8901) & state2==94
		replace fpl=7480+(famsize-1)*2550 if inrange(date,8902,9001) & state2==94
		replace fpl=7840+(famsize-1)*2680 if inrange(date,9002,9101) & state2==94
		replace fpl=8290+(famsize-1)*2820 if inrange(date,9102,9201) & state2==94
		replace fpl=8500+(famsize-1)*2980 if inrange(date,9202,9301) & state2==94
		replace fpl=8700+(famsize-1)*3080 if inrange(date,9302,9401) & state2==94
		replace fpl=9200+(famsize-1)*3100 if inrange(date,9402,9501) & state2==94
		replace fpl=9340+(famsize-1)*3200 if inrange(date,9502,9601) & state2==94
		replace fpl=9660+(famsize-1)*3280 if inrange(date,9602,9702) & state2==94        	
		replace fpl=9870+(famsize-1)*3400 if inrange(date,9703,9802) & state2==94
		replace fpl=10070+(famsize-1)*3500 if inrange(date,9803,9903) & state2==94
		replace fpl=10320+(famsize-1)*3520 if inrange(date,9904,10002) & state2==94
		replace fpl=10430+(famsize-1)*3630 if inrange(date,10003,10102) & state2==94
		replace fpl=10730+(famsize-1)*3780 if inrange(date,10103,10201) & state2==94
		replace fpl=11080+(famsize-1)*3850 if inrange(date,10202,10301) & state2==94
		replace fpl=11210+(famsize-1)*3930 if inrange(date,10302,10401) & state2==94
		replace fpl=11630+(famsize-1)*3980 if inrange(date,10402,10502) & state2==94
		replace fpl=11950+(famsize-1)*4080 if inrange(date,10503,10601) & state2==94
		replace fpl=12250+(famsize-1)*4250 if inrange(date,10602,10701) & state2==94
		replace fpl=12770+(famsize-1)*4350 if inrange(date,10702,10801) & state2==94

		*Hawaii, 95
		replace fpl=3930+(famsize-1)*1260 if inrange(date,7905,8003) & state2==95
		replace fpl=4370+(famsize-1)*1400 if inrange(date,8004,8102) & state2==95
		replace fpl=4980+(famsize-1)*1580 if inrange(date,8103,8203) & state2==95
		replace fpl=5390+(famsize-1)*1770 if inrange(date,8204,8301) & state2==95				
		replace fpl=5600+(famsize-1)*1930 if inrange(date,8302,8401) & state2==95
		replace fpl=5730+(famsize-1)*2000 if inrange(date,8402,8502) & state2==95
		replace fpl=6040+(famsize-1)*2070 if inrange(date,8503,8601) & state2==95
		replace fpl=6170+(famsize-1)*2160 if inrange(date,8602,8701) & state2==95
		replace fpl=6310+(famsize-1)*2190 if inrange(date,8702,8801) & state2==95
		replace fpl=6650+(famsize-1)*2250 if inrange(date,8802,8901) & state2==95
		replace fpl=6870+(famsize-1)*2350 if inrange(date,8902,9001) & state2==95
		replace fpl=7230+(famsize-1)*2460 if inrange(date,9002,9101) & state2==95
		replace fpl=7610+(famsize-1)*2600 if inrange(date,9102,9201) & state2==95
		replace fpl=7830+(famsize-1)*2740 if inrange(date,9202,9301) & state2==95
		replace fpl=8040+(famsize-1)*2820 if inrange(date,9302,9401) & state2==95
		replace fpl=8470+(famsize-1)*2850 if inrange(date,9402,9501) & state2==95
		replace fpl=8610+(famsize-1)*2940 if inrange(date,9502,9601) & state2==95
		replace fpl=8910+(famsize-1)*3010 if inrange(date,9602,9612) & state2==95        	
		replace fpl=9070+(famsize-1)*3130 if inrange(date,9703,9802) & state2==95   
		replace fpl=9260+(famsize-1)*3220 if inrange(date,9803,9903) & state2==95
		replace fpl=9490+(famsize-1)*3240 if inrange(date,9904,10002) & state2==95
		replace fpl=9590+(famsize-1)*3340 if inrange(date,10003,10102) & state2==95
		replace fpl=9890+(famsize-1)*3470 if inrange(date,10103,10201) & state2==95
		replace fpl=10200+(famsize-1)*3540 if inrange(date,10202,10301) & state2==95
		replace fpl=10330+(famsize-1)*3610 if inrange(date,10302,10401) & state2==95
		replace fpl=10700+(famsize-1)*3660 if inrange(date,10402,10502) & state2==95
		replace fpl=11010+(famsize-1)*3750 if inrange(date,10503,10601) & state2==95
		replace fpl=11270+(famsize-1)*3910 if inrange(date,10602,10701) & state2==95
		replace fpl=11750+(famsize-1)*4000 if inrange(date,10702,10801) & state2==95
			
		*monthly value of FPL
		replace mfpl=fpl/12
		replace minc=(fpl*(fplfinc/100))/12
		replace mearn=(fpl*(fplfearn/100))/12

		*merging on AFDC parameters
		if inrange(year,1979,1996){
			merge date state2 using src/afdcrules7996, sort ///
			uniqusing nokeep update replace
			*check
			tab _merge
			drop _merge
		}
		if inrange(year,1997,2007){
			merge state2 using src/afdcjuly1996.dta, sort uniqusing ///
			nokeep update replace
			tab _merge
			drop _merge
		}

		*assigning the relevant parameter based on family size to each family
		gen ns=.
		gen mb=.
		gen ps=.		
		forval p=1/12{
			replace ns=ns`p' if famsize==`p'
			replace mb=mb`p' if famsize==`p'
			replace ps=ps`p' if famsize==`p'
		}
		replace ns=ns12 if inrange(famsize,13,100)
		replace mb=mb12 if inrange(famsize,13,100)
		replace ps=ps12 if inrange(famsize,13,100)

		************************************************************************

		*Calculating AFDC benefits 

		*Determining FLAT and FRACT AFDC disregard components based upon
		*months spent working
		gen wmonth=1
		replace flat=0 if wmonth>flatmth
		replace fract=0 if wmonth>fractmth

		*Second, calculate work expense deduction
		gen wed=wedflat if inlist(wedrule,2,3,5)
		replace wed=min(mearn*wedfract,wedflat) if wedrule==4
		replace wed=min(mearn,wedflat + (mearn-wedflat)*wedfract) if wedrule==6
		gen wedben=0
		replace wedben=wed if inlist(weduse,2,3)
		gen wednit=0
		replace wednit=wed if inlist(weduse,1,3)

		*Third, calculate disregard
		gen disregrd=(flat+wedben+ccd) + (fract*(mearn-flat-wedben-ccd)) ///
			if dsrgrule==2
		replace disregrd=(flat+wedben+ccd) +(fract*(mearn-flat-wedben)) ///
			if dsrgrule==3

		*Calculating disregard for Medicaid expansions 
		gen dis2=ccd+cond(wedflat,inlist(wedrule,2,3,5),0)

		*Fourth, calculate AFDC benefit
		gen afdc=max(0,min(mb,defpay*(ps-(minc-disregrd))))

		*Fifth, apply net income test
		replace afdc=0 if (minc-((dsrgrule==2)*(flat+wednit+ccd+fract*(mearn-flat-wednit-ccd)))- ///
		  ((dsrgrule==3)*(flat+wednit+ccd+fract*(mearn-flat-wednit))))>ns

		*Sixth, apply gross income test;
		replace afdc=0 if minc>(grosscut*ns)

		*Merge on parameters for determining Medicaid/SCHIP eligibility
		merge state2 year using src/medschiprules, _merge(medmerge) ///
		sort uniqusing nokeep update replace
		tab medmerge
		drop medmerge			

		*Poverty-related Medicaid expansions
		rd_medicaid5
		gen melig$date=melig
		gen schpelig$date=schpelig
		gen fmelig$date=fmelig

		*Special rule for CT in 1996
		replace afdc=mb if (state2==16 & mearn<fpl/12) & year==1996
		replace afdc=0 if (state2==16 & mearn>fpl/12) & year==1996

		******Now coding in Ribicoff states*****
		gen ribi=0

		replace ribi=1 if agem<=19 & inlist(state2,86,58,33,61,64,88,74,87,91,35) & inrange(year,1979,1985)
		replace ribi=1 if agem<=21 & inlist(state2,94,93,16,51,53,95,82,42,11,14,34,46,22,21,56,44,73,92,23,13) & inrange(year,1979,1985)
		*ed LW, 8/28/09
		*ed LW, 1/14/10: for now assume same laws in place in 1982-1984 as in 1985
		*ed LW, 2/25/13: assume same laws in place back to 1979, unable to find sources for Ribicoff rules during this time period

		replace ribi=1 if agem<=19 & inlist(state2,86,58,33,61,64,88,15,74,87,91,35) & year==1986
		replace ribi=1 if agem<=21 & inlist(state2,94,93,16,51,53,95,82,42,11,14,34,46,22,21,56,44,73,92,23,57,62,13) & year==1986
		*ed LW, 8/28/09

		replace ribi=1 if agem<=18 & inlist(state2,86,71,59,58,33,47,64,56,57,87,35) & year==1987
		replace ribi=1 if agem<=19 & state2==74 & year==1987
		replace ribi=1 if agem<=20 & state2==46 & year==1987
		replace ribi=1 if agem<=21 & inlist(state2,94,93,16,53,42,11,52,14,34,41,43,22,21,44,31,73,23,62,13) & year==1987
		*ed LW, 8/28/09

		replace ribi=1 if agem<=18 & inlist(state2,86,71,59,64,87,35,58,33,47,57,56) & year==1988
		replace ribi=1 if agem<=19 & state2==74 & year==1988
		replace ribi=1 if agem<=20 & state2==46 & year==1988
		replace ribi=1 if agem<=21 & inlist(state2,93,16,53,42,11,52,14,41,22,21,44,31,73,23,62,13,71,34,43) & year==1988

		replace ribi=1 if agem<=18 & inlist(state2,86,71,59,64,87,35,58,33,47,57) & year==1989
		replace ribi=1 if agem<=19 & state2==74 & year==1989
		replace ribi=1 if agem<=21 & inlist(state2,94,93,16,53,42,11,52,14,41,22,21,56,44,31,73,23,62,13,34,46) & year==1989

		replace ribi=1 if agem<=18 & inlist(state2,86,71,59,64,87,91,35) & year>=1990
		replace ribi=1 if agem<=19 & inlist(state2,61,57,55) & year>=1990
		replace ribi=1 if agem<=20 & state2==46 & year>=1990
		replace ribi=1 if agem<=21 & inlist(state2,94,93,16,51,53,42,11,52,14,41,22,21,56,44,31,73,23,62,13) & year>=1990

		******Incorporating AFDC-UP information by year******
		*Imposing max 1200 hours per year to qualify for AFDC-UP
		*afdcup measures eligibility for UP without considering income i.e. just checking for hours and state
		gen afdcup=1*(married)
		replace afdcup=0 if hours>1200 
		
		replace afdcup=0 if ~inlist(state2,93,84,16,51,53,95,33,42,47,52,14,34,41,43,81,46,22, ///
			21,31,92,23,15,87,13,91,55,35) & year==1979 & married==1
		replace afdcup=0 if ~inlist(state2,93,84,16,51,53,95,33,42,47,52,14,34,41,43,81,46,22, ///
			21,31,23,15,87,13,91,55,35) & year==1980 & married==1
		replace afdcup=0 if ~inlist(state2,93,84,16,51,53,95,33,47,52,14,34,41,81,46,22, ///
			21,31,23,15,13,55,35) & year==1981 & married==1		
		*ed 2/25/13, LW
		
		replace afdcup=0 if ~ inlist(state2,93,84,16,51,53,95,33,42,47,52,14,34,41,46,22,21,31, ///
			23,15,13,55,35) & year==1982 & married==1
		replace afdcup=0 if ~ inlist(state2,93,84,16,51,53,95,33,42,47,52,14,34,41,43,46,22,21,31, ///
			23,15,13,91,55,35) & year==1983 & married==1	
		replace afdcup=0 if ~ inlist(state2,93,84,16,51,53,95,33,42,47,11,52,14,34,41,43,46,22,21,31, ///
			23,15,13,91,55,35) & year==1984 & married==1	
		replace afdcup=0 if ~ inlist(state2,93,16,51,53,95,33,42,47,11,52,14,34,41,43,81,46,22,21,31, ///
			23,15,57,13,91,55,35) & year==1985 & married==1	
		replace afdcup=0 if ~ inlist(state2,93,16,51,53,95,33,42,47,11,52,14,34,41,43,81,46,22,21,31, ///
			92,23,15,57,13,91,55,35) & inrange(year,1986,1987) & married==1
		*ed 1/14/10, LW
		
		replace afdcup=0 if ~ inlist(state2,93,16,53,51,59,58,95,33,42,47,61,72,11,52,14,34,41, ///
		43,81,46,22,21,31,92,23,15,57,13,91,55,35) & year==1988 & married==1
		replace afdcup=0 if ~ inlist(state2,93,16,53,51,59,58,95,33,42,47,61,72,11,52,14,34,41, ///
		  43,81,46,22,21,31,92,23,15,57,13,91,55,35,56,73) & year==1989 & married==1
		replace afdcup=0 if ~ inlist(state2,93,16,53,51,59,58,95,33,42,47,61,72,11,52,14,34,41, ///
		  43,81,46,22,21,31,92,23,15,57,13,91,55,35,56,73,83) & inrange(date,9001,9009) & married==1	

		*From October 1, 1990 onward all states had AFDC-UP. No eligibility if no kids.*
		replace afdc=0 if skidafdc==0
		replace afdcup=0 if afdcup==.

		******Calculating elig under AFDC, AFDC-UP, Ribicoff, Med Expansions****
		* upelig is eligibility for AFDCUP, aelig is elig for traditional AFDC (single parent)
		* ribelig is ribicoff eligibility and melig was eligibility through state programs 
		* as calculated by medicaid.ado
		gen upelig$date=1*(afdc>0 & afdcup & ~post1997)
		gen aelig$date=1*(afdc>0 & ~married & ~post1997)
		gen ribelig$date=1*(ribi & afdc>0)
		*Special rule for Minnesota
		replace aelig$date=1 if state2==41 & date>=8707 & minc<=1.33*ns & skidafdc>0 & ~married & ~post1997
		*DEFRA 1984
		replace melig$date=1 if birthday>=8310 & agem<5 & afdc>0 & date>=8410
		gen exp1$date=1*(birthday>=8310 & agem<5 & afdc>0 & date>=8410)
		*OBRA 1987
		replace melig$date=1 if birthday>=8310 & agem<7 & afdc>0 & date>=8810
		replace exp1$date=1 if birthday>=8310 & agem<5 & afdc>0 & date>=8410
		gen secelig$date=1*(afdc>0 & (~married | afdcup) & post1997)

		*Determine final eligibility status!
		gen	medelig$date=1*(aelig$date | melig$date | schpelig$date | upelig$date | ribelig$date | secelig$date)
end

/******************************************************************************
C) ESTIMATE ELIGIBILITY FOR EACH MONTH OF LIFETIME (AGES 0-17)
FOR SEPTEMBER 1983 AND OCTOBER 1983 COHORTS
******************************************************************************/
forval q=1984/1984{
	forval x=9/10{
		use data/rd_simstatepre, clear	
		
        local county=(`q'-1901)*100
        di "`county'"
        replace birthday=`county'+`x'
        local data=`county'+`x'
        replace bmonth=`x'
        replace byear=`q'-1
		di "cohort `data'"
		save data/rd_simstate`data', replace
		
		*calculate lifetime eligibility for child for ages 17 and younger
        forval y=1979/2005{
	    	forval m=1/12{
		        *assign date of eligibility determination
				replace date=(`y'-1900)*100+`m'
				global date=(`y'-1900)*100+`m'
				replace year=`y'
				gen post1997=1*(`y'>=1997)

		        *assign age at date of eligibility determination
				gen agem$date=.
				replace agem$date=`y'-byear if bmonth<=`m'
				replace agem$date=`y'-byear-1 if bmonth>`m'
				gen agem=agem$date

				*call program to calculate eligibility
				rd_eligibility
				
				*drop variables no longer need
				drop ns* mb* ps* wmonth flat fract flatmth fractmth wed ///
				wedflat wedfract wedrule weduse wedben wednit disregrd dis2 afdc ///
				defpay grosscut dsrgrule ribi afdcup pwi fmedpct smedpct ///
				medpct medflat medfract chpflat minc2 schipct post1997 ///
				melig schpelig agem fmelig

				save data/rd_simstate`data', replace
			}
		}
	}
}

/******************************************************************************
D) CALCULATE DISCONTINUITY IN CHILDHOOD ELIGIBILITY AT BIRTHDATE CUTOFF
******************************************************************************/
foreach x in 8310 8309 {
	
        use data/rd_simstate`x', clear

        local med medelig7812
        local age agem7812

        *Go through age 17
        forval y=1979/2005{
	       	forval m=1/12{
            	local date=(`y'-1900)*100+`m'
            	local med `med' medelig`date'
            	local age `age' agem`date'
				*only looking through 17 year olds
            	replace medelig`date'=0 if ~inrange(agem`date',0,17)
			}
		}
        gen medelig7812=.
        gen agem7812=.
        
        *calculate cumulative years of eligibility
        egen moselig=rowtotal(`med')
        egen totmos=anycount(`age'), v(0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17)
        su moselig
        su totmos
		gen yrselig`x'=moselig/12

		keep iobs race yrselig`x' state2 wgt

		save data/rd_simstatesum`x',replace
}

***state estimates for Appendix Table 1
use data/rd_simstatesum8310,clear
merge 1:1 iobs using data/rd_simstatesum8309, keep(match) nogen
gen difyrs=yrselig8310-yrselig8309
gen ob=1
save data/temp, replace
collapse difyrs_all=difyrs (rawsum) n_all=ob [pweight=wgt], by(state2)
save data/temp2, replace
use data/temp, clear
gen black=1*(race==2)
collapse difyrs (rawsum) n=ob [pweight=wgt], by(state2 black)
reshape wide difyrs n, i(state2) j(black)
rename difyrs1 difyrs_black
rename n1 n_black
rename difyrs0 difyrs_nonblack
rename n0 n_nonblack
merge 1:1 state2 using data/temp2, keep(match using) nogen
export excel using output/rd_state_elig, replace first(var)

***national estimates for Table 1
use data/temp, clear
gen pre=1*(yrselig8309>0)
gen post=1*(yrselig8310>0)
*adjust for advantage of being born one month earlier
gen gain=1*(difyrs>.1)
gen gainamt=difyrs if gain
replace gainamt=. if difyrs<=0
save data/temp2, replace
collapse pre post gain difyrs gainamt [pweight=wgt]
save data/temp3, replace
use data/temp2, clear
gen black=1*(race==2)
collapse pre post gain difyrs gainamt [pweight=wgt], by(black)
append using data/temp3
export excel using output/rd_race_gain, replace first(var)

